【问题标题】:MediaPlayer giving error (262,0)MediaPlayer 给出错误 (262,0)
【发布时间】:2016-10-09 17:50:39
【问题描述】:

我是一个非常初学者,并试图制作我的第一个应用程序,按钮在按下时播放声音......我认为是一个音板。

E/MediaPlayer:应该已经设置了字幕控制器

E/MediaPlayer:错误 (262,0)

当我第二次点击按钮时:

E/MediaPlayer:错误 (-38,0)

这些是我用这个简单的代码得到的错误:

public class MainActivity extends AppCompatActivity {protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final MediaPlayer m1 = MediaPlayer.create(getApplicationContext(), R.raw.s1);
    Button b1 = (Button) findViewById(R.id.s1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            m1.start();
        }
    });}

“s1.mp3”在原始文件夹中,大约 3-4 秒长,我认为这不是问题...... 我不明白我做错了什么 - 一个在线教程教会了我所有这些,有一个类似的代码...... 请帮助我,正如我所说,我是一名初学者开发人员,所以我需要帮助...... 提前谢谢!

更新:

public class MainActivity extends AppCompatActivity {
MediaPlayer m1;
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    m1 = MediaPlayer.create(MainActivity.this, R.raw.s1);
    Button b1 = (Button) findViewById(R.id.s1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            m1.start();
        }
    });}
 }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.antiriad7.zviadi.MainActivity"
android:orientation="vertical">
<LinearLayout
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:layout_weight="25"
    android:id="@+id/l1">
    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S1"
        android:id="@+id/s1"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="34"
        android:text="S2"
        android:id="@+id/s2"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S3"
        android:id="@+id/s3"
        android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="25"
    android:layout_below="@+id/l1"
    android:id="@+id/l2">

    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S4"
        android:id="@+id/s4"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="34"
        android:text="S5"
        android:id="@+id/s5"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S6"
        android:id="@+id/s6"
        android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/l2"
    android:layout_weight="25"
    android:id="@+id/l3">

    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S7"
        android:id="@+id/s7"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="34"
        android:text="S8"
        android:id="@+id/s8"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S9"
        android:id="@+id/s9"
        android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/l3"
    android:layout_weight="25"
    android:id="@+id/l4">

    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S10"
        android:id="@+id/s10"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="34"
        android:text="S11"
        android:id="@+id/s11"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="33"
        android:text="S12"
        android:id="@+id/s12"
        android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>

【问题讨论】:

  • 点击按钮时你的mp3歌曲是否正在播放??
  • 不,它不播放...
  • 查看我的更新答案

标签: android button audio


【解决方案1】:

当 MediaPlayer 开始播放音乐时,它会检查是否有 SubtitleController,如果没有设置则显示此消息。它似乎并不关心您要播放的源是音乐还是视频。不知道他为什么这样做。

不要在意这个“例外”。

要删除此异常,请按照以下步骤操作:

  1. 右键单击曲目(您的原始文件夹 .mp3 文件)
  2. 然后选择属性
  3. 选择详细信息
  4. 然后在字幕上插入歌曲文本(标题)
  5. 完成

更新

public class MainActivity extends AppCompatActivity {

Mediaplayer m1; 

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m1 = MediaPlayer.create(MainActivity.this, R.raw.s1);
Button b1 = (Button) findViewById(R.id.s1);
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        m1.start();
    }
});}

希望对你有用

【讨论】:

  • 我无法编辑字幕...也许我应该从 android studio 本身做点什么?这会修复并让按钮播放声音吗?
  • 不关心这个异常。你按照我的回答更新你的代码了吗??
  • 并全局声明您的媒体播放器变量
  • 我已经更新了,但还是一样。 “全球”是什么意思?
  • 只需在您的 mainactivity 中复制并过去我更新的答案,然后在原始文件夹中添加一首歌曲....它肯定可以工作
【解决方案2】:

保持您的 XML 不变,然后将您的 MainActivity 代码更改为:

    public class MainActivity extends AppCompatActivity {

    MediaPlayer m1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m1 = MediaPlayer.create(MainActivity.this, R.raw.s1);
        Button b1 = (Button) findViewById(R.id.s1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                m1.start();
            }
        });
    }
}

然后在 Android Studio 中做一个 Build --> Clean Project,然后运行它。它有效吗?什么错误?

【讨论】:

  • 我第一次点击时得到 E/MediaPlayer: error (262, 0)。手机上的应用程序本身不会崩溃,但不会播放声音。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多