Android:媒体播放器创建 [英] Android: mediaplayer create

查看:53
本文介绍了Android:媒体播放器创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

package com.example.pr;

import android.media.MediaPlayer;

public class Audio{

    MediaPlayer mp;

    public void playClick(){
        mp = MediaPlayer.create(Audio.this, R.raw.click);  
        mp.start();
    }
}

我在创建"中有一个错误消息MediaPlayer 类型中的方法 create(Context, int) 不适用于参数 (Audio, int)"

I have an error in "create" with this message "The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Audio, int)"

为什么?

推荐答案

MediaPlayer.create() 需要一个 Context 作为第一个参数.传入当前的 Activity,它应该可以工作了.

MediaPlayer.create() needs a Context as first parameter. Pass in the current Activity and it should work.

试试:

public void playClick(Context context){
    mp = MediaPlayer.create(context, R.raw.click);  
    mp.start();
}

在您的活动中:

audio = new Audio();
...
audio.playClick(this);

但是不要忘记在声音完成后在 MediaPlayer 实例上调用 release ,否则你会得到一个异常.

but don't forget to call release on the MediaPlayer instance once the sound has finished, or you'll get an exception.

但是,无论如何,使用 SoundPool 播放短时点击可能会更好.

However, for playing short clicks using a SoundPool might be better anyway.

这篇关于Android:媒体播放器创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆