让soundclip在ListView可选择为(通知)铃声? [英] Make soundclip in a listView selectable as (notification) ringtone?

查看:230
本文介绍了让soundclip在ListView可选择为(通知)铃声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我做了色调的名单,我已经成功地让他们到一个列表,但现在我面对允许用户点击并按住分配作为挑战通知音。下面是从MainActivity.java的code:

 进口的java.util.ArrayList;
进口android.app.ListActivity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.ContextMenu;
进口android.view.ContextMenu.ContextMenuInfo;
进口android.view.MenuInflater;
进口android.view.View;
进口android.widget.ListView;
公共类MainActivity扩展ListActivity {
私人ArrayList的<声音> mSounds = NULL;
私人SoundAdapter mAdapter = NULL;
静态的MediaPlayer mMediaPlayer = NULL;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView()setSelector(R.drawable.selector)。
//创建一个简单的列表
mSounds =新的ArrayList<声音>();
声音S =新的Sound();
s.setDescription(Anjels);
s.setSoundResourceId(R.raw.anjels);
mSounds.add(多个);
S =新的Sound();
s.setDescription(仇恨);
s.setSoundResourceId(R.raw.aggro);
mSounds.add(多个);
S =新的Sound();
s.setDescription(AXO);
s.setSoundResourceId(R.raw.axo);
mSounds.add(多个);
S =新的Sound();
s.setDescription(BASIX);
s.setSoundResourceId(R.raw.basix);
mSounds.add(多个);
S =新的Sound();
s.setDescription(德尔);
s.setSoundResourceId(R.raw.bender);
mSounds.add(多个);
mAdapter =新SoundAdapter(这一点,R.layout.list_row,mSounds);
setListAdapter(mAdapter);
}
@覆盖
公共无效onListItemClick(ListView的父母,视图V,INT位置,长的id){
声音S =(声音)mSounds.get(位置);
MediaPlayer的熔点= MediaPlayer.create(在此,s.getSoundResourceId());
mp.start();@覆盖
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
    super.onCreateContextMenu(菜单,V,menuInfo);
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.context_menu,菜单);
  }
}

我也有另外两个java文件,一个是声类,另一个是声音适配器虽然我不认为他们是相对于我的问题。

只是让你们知道,这是我花几个星期来得到这个据我仍然在javaland一年级学生。任何帮助或这里的例子是热的!地狱如果你想为我做的工作那么这是更甜蜜!


解决方案

  

我发布的解决方案只是让你有可能检测
  该列表项的位置,你点击,并设置为铃声(没有工作),里面的 setRingtone 函数的奇迹发生。所有code使声音为铃声必须放在那里。


  
  

开关案例IDMAIN_MENU修改项目编号菜单c> - > context_menu.xml 像下面


  
  

 <?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
    机器人:ID =@ + ID / MAIN_MENU
    机器人:标题=@字符串/ set_ringtone
    机器人:showAsAction =ifRoom
    />
&所述; /菜单>


  

下添加 onCreateContextMenu 的code以下。

  @覆盖
 公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
     super.onCreateContextMenu(菜单,V,menuInfo);
     MenuInflater吹气= getMenuInflater();
     inflater.inflate(R.menu.context_menu,菜单);
   }
 } / * -----------------------------下面添加code ------------- ----------------- * / @覆盖
    公共布尔onContextItemSelected(菜单项项){
        AdapterView.AdapterContextMenuInfo信息;
        尝试{
            信息=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        }赶上(抛出ClassCastException E){
            Log.e(,坏menuInfo,E);
            返回false;
        }        开关(item.getItemId()){
            案例R.id.main_menu:
                setRingtone(item.getItemId(),info.position);
                //Toast.makeText(this,ID =+ item.getItemId()+POS =+ info.position,Toast.LENGTH_SHORT).show();
                打破;
            默认:
                返回false;
        }        返回true;
    }    公共无效setRingtone(INT ID,INT POS){
        //RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_RINGTONE,newUri);        Toast.makeText(这一点,ID =+身份证+POS =+ POS,Toast.LENGTH_SHORT).show();
    }

I've got a list of tones which I've made, I've managed to get them into a list but now I'm faced with the challenge of allowing the user to "touch and hold" to assign as a notification tone. Here's the code from MainActivity.java:

       import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Axo");
s.setSoundResourceId(R.raw.axo);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }
}

I also have two other java files, one is a "sound class" and the other a "sound adapter" although I don't think they are relative to my question.

Just so you guys know, it's taken me weeks to get this far as I'm still a freshman in javaland. Any help or examples here would be hot! Hell if you wanna do the work for me then that's even sweeter!

解决方案

The solution I post is just making it possible for you to detect the position of the listitem you clicked and set as ringtone (which is not working), inside the setRingtone function the magic happens. all code to make the sound as ringtone must be placed in there

Inside the switch change the case id 'main_menu' to the item id from your menu -> context_menu.xml like below

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/main_menu"
    android:title="@string/set_ringtone"
    android:showAsAction="ifRoom"
    />
</menu>

Add beneath onCreateContextMenu the code below.

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.context_menu, menu);
   }
 }

 /*-----------------------------  add code below ------------------------------*/

 @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info;
        try {
            info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        } catch (ClassCastException e) {
            Log.e("", "bad menuInfo", e);
            return false;
        }

        switch (item.getItemId()){
            case R.id.main_menu:
                setRingtone(item.getItemId(), info.position);
                //Toast.makeText(this, "id = " + item.getItemId() + " pos =     " + info.position, Toast.LENGTH_SHORT).show();
                break;
            default:
                return false;
        }

        return true;
    }

    public  void setRingtone(int id, int pos){
        //RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);

        Toast.makeText(this, "id = " + id + " pos = " + pos, Toast.LENGTH_SHORT).show();
    }

这篇关于让soundclip在ListView可选择为(通知)铃声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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