简体   繁体   English

MediaPlayer错误

[英]Error with MediaPlayer

I am trying to make music play in my app and have a song play after the first one has finished but i get some errors that i cannot resolve any help? 我正在尝试在我的应用中播放音乐,并在第一个播放完成后播放歌曲,但是出现一些我无法解决任何帮助的错误?

public class Music {
    int count;
    String[] titles = new String[] { "title1.mp3", "title2.mp3", "title3.mp3", "title4.mp3" };

    public void GameMusic(){
        count = 0;
        MediaPlayer mp = MediaPlayer.create(this, R.raw.title1);
        mp.start();
    }

    void onCompletion(MediaPlayer mp){
        mp.stop();
        if (count == titles.length -1) {
            count = 0;
        }
        mp.setDataSource(titles[count]);
        count++;
        mp.prepare(); 
        mp.start();
    }
}

The errors are on: 错误发生在:

    MediaPlayer mp = MediaPlayer.create(this, R.raw.music); 

(The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Music, int) (MediaPlayer类型的create(Context,int)方法不适用于参数(Music,int)

    mp.setDataSource(titles[count]);

(Unhandled exception type IOException) (未处理的异常类型IOException)

    mp.prepare();

(Unhandled exception type IOException) (未处理的异常类型IOException)

Any help would be appreciated. 任何帮助,将不胜感激。

Well the errors might be a little cryptic but they are self-explanatory. 好吧,这些错误可能有点神秘,但它们是不言而喻的。

You need to pass a Context to GameMusic() and use it to initialize MediaPlayer: 您需要将Context传递给GameMusic()并使用它来初始化MediaPlayer:

public void GameMusic(Context context){
    count = 0;
    MediaPlayer mp = MediaPlayer.create(context, R.raw.title1);
    mp.start();
}

this only works if the class (in this case Music) extends Context, so you need to provide a Context another way. this仅在类(在本例中为Music)扩展了Context的情况下有效,因此您需要以其他方式提供Context。

And in onCompletion() use a try-catch block to handle the IOException: 在onCompletion()中,使用try-catch块来处理IOException:

try {
    mp.setDataSource(titles[count]);
    count++;
    mp.prepare(); 
    mp.start();
}
catch(IOException e) {
    // Do something when MediaPlayer fails
}

Your Music Class is just a class and not an activity and hence the this passes an object and not a context . 您的音乐课只是一堂课,而不是一项活动,因此, this通过一个对象而不是context If this is your only music player class then you need it to extend Activity else you need to pass a context to it. 如果这是您唯一的音乐播放器类,则需要它来扩展Activity否则您需要向其传递上下文。

public void GameMusic(Context context){
count = 0;
MediaPlayer mp = MediaPlayer.create(context, R.raw.title1);
mp.start();
}

And for the other two exceptions, it states that the exceptions are unhandled. 对于其他两个异常,它指出未处理的异常。 so you need to use a Try-Catch block the handle the uncaught IOException . 因此,您需要使用Try-Catch块来处理未捕获的IOException

That should solve all the errors that you are getting now. 那应该可以解决您现在遇到的所有错误。

when setting the datasource to Mediaplayer try to use absolutepath of the music file 将数据源设置为Mediaplayer时,尝试使用音乐文件的绝对路径

if it is stored in sd-card means ..try this 如果存储在SD卡中..尝试

 String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory()
                                      .getAbsolutePath();

 mp.setDataSource(ExternalStorageDirectoryPath +"/"+titles[count]);

after calling the mp.prepare(); 调用mp.prepare()之后;

implement the onpreparedlistner ,after get the notification for this then start the mediaplayer 实现onpreparedlistner,在获得通知后,然后启动mediaplayer

 @Override
 public void onPrepared(MediaPlayer mp) {
   mp.start();
 }

and handle the all kind of exception, so that u can easily track the issues. 并处理所有异常,以便您可以轻松跟踪问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM