【问题标题】:All mp3 files in raw folder played播放原始文件夹中的所有 mp3 文件
【发布时间】:2016-06-30 04:53:23
【问题描述】:

我用一个布局和两个按钮构建了一个简单的应用程序,这是我的代码..

package com.example.tessound;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener
{

MediaPlayer player;
Button play,mute;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play = (Button)findViewById(R.id.button1);
    play.setOnClickListener(this);
    mute = (Button)findViewById(R.id.button2);
    mute.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View view)
{
    if(view.getId()==R.id.button1)
    {
        playSound(1);
    }
    else if(view.getId()==R.id.button2)
    {
        playSound(2);
    }
}

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying()) 
            {
                player.stop();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 1)
    {
        player = MediaPlayer.create(this, R.raw.atur);
    }
    else if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.back);
    }

    if(player != null)
    {
        player.setLooping(false);
        player.start();
    }

    }
}

我的原始文件夹包含: atur.mp3、back.mp3、belajar.mp3、level.mp3、skip.mp3 和 start.mp3... 当我单击按钮静音时,播放 back.mp3,但是当我单击按钮播放时,播放原始文件夹中的所有 mp3.. 任何 cmets?

【问题讨论】:

  • 首先解决你的问题?

标签: android audio


【解决方案1】:

我已经复制了您的代码并对其进行了测试。在我的系统中它工作正常。我刚刚重命名了歌曲和按钮 id ,因为我无法获得那些原始歌曲。请看这段代码,代码是一样的,因为我只是复制了这段代码:

package com.mukherjee.satyaki.test;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity1 extends Activity implements OnClickListener
{

MediaPlayer player;
Button play,mute;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play = (Button)findViewById(R.id.songid1);
    play.setOnClickListener(this);
    mute = (Button)findViewById(R.id.songid2);
    mute.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View view)
{
    if(view.getId()==R.id.songid1)
    {
        playSound(1);
    }
    else if(view.getId()==R.id.songid2)
    {
        playSound(2);
    }
}

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying()) 
            {
                player.stop();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 1)
    {
        player = MediaPlayer.create(this, R.raw.ram1);
    }
    else if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.ram3);
    }

    if(player != null)
    {
        player.setLooping(false);
        player.start();
    }

    }
}

您的代码很好,根据我之前的假设,重新启动模拟器并擦除用户数据,然后进行测试。我认为问题将通过您的代码解决。

谢谢。

【讨论】:

  • 我已经重新启动模拟器,清理,擦除用户数据,当我选择 atur.mp3,level.mp3,skip.mp3 时,它仍然播放所有 mp3.. 当我选择 back.mp3, belajar.mp3,和start.mp3,都没有问题..:|
  • 但是我亲爱的朋友我已经使用了你的代码,它给出了完美的结果。现在我对此一无所知。对不起朋友。
【解决方案2】:

我记得有一个类似的问题,我的代码最终看起来像这样:

if (!getmMediaPlayer().isPlaying()) {
    try {
        AssetFileDescriptor fd = getResources().openRawResourceFd(
                R.raw.snooze);
        getmMediaPlayer().setDataSource(fd.getFileDescriptor(),
                fd.getStartOffset(), fd.getLength());
        getmMediaPlayer().setAudioStreamType(
                AudioManager.STREAM_ALARM);
        getmMediaPlayer().setLooping(true);
        getmMediaPlayer().prepare();
        getmMediaPlayer().start();
    } catch (IllegalStateException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
}

具体来说,我认为 setDataSource 很重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2014-03-04
    相关资源
    最近更新 更多