用MediaPlayer的问题,原材料资源,停止和启动 [英] Problems with MediaPlayer, raw resources, stop and start

查看:163
本文介绍了用MediaPlayer的问题,原材料资源,停止和启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,我有一个问题/问题。

I'm new to Android development and I have a question/problem.

我在摆弄的MediaPlayer类重现一些声音/音乐。我玩的原始资源( RES /生),它看起来那种轻松。

I'm playing around with the MediaPlayer class to reproduce some sounds/music. I am playing raw resources (res/raw) and it looks kind of easy.

要发挥原料资源,在MediaPlayer已经是这样初始化的:

To play a raw resource, the MediaPlayer has to be initialized like this:


MediaPlayer mp = MediaPlayer.create(appContext, R.raw.song);
mp.start();

直到这里是没有问题的。声音过后,一切工作正常。当我想要添加更多的选择,我的应用我的问题出现。特别是当我添加了停止按钮/选项。

Until here there is no problem. The sound is played, and everything works fine. My problem appears when I want to add more options to my application. Specifically when I add the "Stop" button/option.

基本上,我想做的事是什么?当我preSS停止,音乐停止。当我preSS开始,这首歌曲/声音开始了。 (pretty的基础!)

Basically, what I want to do is...when I press "Stop", the music stops. And when I press "Start", the song/sound starts over. (pretty basic!)

要停止媒体播放器,你只需要调用停止()。但是,再次播放声音,媒体播放器有先复位和prepared。

To stop the media player, you only have to call stop(). But to play the sound again, the media player has to be reseted and prepared.


mp.reset();
mp.setDataSource(params);
mp.prepare();

现在的问题是,该方法的setDataSource()只接受作为PARAMS文件路径,内容提供商的URI,流媒体URL路径,或文件描述符。

The problem is that the method setDataSource() only accepts as params a file path, Content Provider URI, streaming media URL path, or File Descriptor.

因此​​,由于这种方法不接受资源标识符,我不知道如何设置数据源,以调用 prepare() 。另外,我不明白为什么你不能用种源标识符来设置数据源,但在初始化的MediaPlayer时,你可以使用一个资源标识符。

So, since this method doesn't accept a resource identifier, I don't know how to set the data source in order to call prepare(). In addition, I don't understand why you can't use a Resouce identifier to set the data source, but you can use a resource identifier when initializing the MediaPlayer.

我想我失去了一些东西。我不知道如果我是混合概念,方法停止()没有被称为停止按钮。任何帮助?

I guess I'm missing something. I wonder if I am mixing concepts, and the method stop() doesn't have to be called in the "Stop" button. Any help?

在此先感谢!

推荐答案

下面是我做加载多个资源,一个MediaPlayer的:

Here is what I did to load multiple resources with a single MediaPlayer:

/**
 * Play a sample with the Android MediaPLayer.
 *
 * @param resid Resource ID if the sample to play.
 */
private void playSample(int resid)
{
    AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);

    try
    {   
        mediaPlayer.reset();
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
        afd.close();
    }
    catch (IllegalArgumentException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }
    catch (IllegalStateException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }
    catch (IOException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }

无限灵是获得创建,并在类中的其他点发布了一个成员变量。这可能不是最好的方法(我是新来的Andr​​oid我自己),但它似乎工作。只是注意,code可能还会下降波谷到方法的底部MediaPlayer正在完成播放前。如果你需要打一系列的资源,你仍然需要处理这种情况。

mediaPlay is a member variable that get created and released at other points in the class. This may not be the best way (I am new to Android myself), but it seems to work. Just note that the code will probably fall trough to the bottom of the method before the mediaPlayer is done playing. If you need to play a series of resources, you will still need to handle this case.

这篇关于用MediaPlayer的问题,原材料资源,停止和启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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