Category: Tips

การทำงานกับเสียงใน iOS

หลังจากหาข้อสรุปมาช่วงนึง ก็ได้เวลาหาข้อมูลจริงๆ จัง จนพอได้ข้อสรุปที่ใช้เป็นแนวทางได้ จึงอยากเอามาแชร์ให้ทุกคน เผื่อจะได้ประหยัดเวลาในการเลือกใช้ api ต่างๆ ที่มีมาให้ (เยอะเหลือเกิน) ใน iOS

Objective-C (iOS)  working with audio:

  • OpenAL :  when you want a convenient, high-level interface for positioning sounds in a stereo field or when you need low latency playback.
  • Audio File Stream Services  :  to parse audio packets from a file or a network stream.
  • AVAudioPlayer class  :  For simple playback of single or multiple sounds
  • AVAudioRecorder class  :  For recording to a file
  • Voice Processing I/O unit  :  for audio chat
  • iPod Library Access  :  To play audio resources synced from a user’s iTunes library
  • Core Audio’s System Sound Services  :  When your sole audio need is to play alerts and user-interface sound effects.
  • Audio Queue Services  :  For other audio applications, including playback of streamed audio, precise synchronisation, and access to packets of incoming audio.

ข้อมูลเพิ่มเติม อ่านต่อได้ ที่นี่

Get system time and check time pass in Objective-C

Tips: เราสามารถเช็ค ระยะเวลาการประมวลผลของคำสั่งในแต่ละบรรทัด หรือบล็อคของคำสั่ง ได้ง่ายๆ ด้วยวิธี:

NSDate *startDate = [NSDate date];
….

// processing something here …

NSTimeInterval elapsedTimeInterval = [startDate timeIntervalSinceNow];
NSLog(@”>>>>>>>>>>>>>>>> = %f”, elapsedTimeInterval);

ผลลัพธ์ที่ได้เป็นหน่วยวินาที (ติดลบ)

Connect Camera is easy, turn it off is evil!!

To turn it on is so simple as document said but to turn it off after access need specific method. After google around I found this one solution:

video.attachCamera(null);
video.clear();
removeChild(_video);
camera = null;

ref: http://www.ninjabonsai.net/blog/?p=80

I test in an empty fla and it work like a charm. But when I test in my working project which do a lot more of connect and display video. I found that another important trick to do is below:

var cam:Camera = Camera.getCamera( id );
video.attachCamera(null);  // this need to set before the next line, always.
video.attachCamera(cam);

I know it weird but it work well for me.

หลุมพลาง Auto Scaling feature จาก corona

เคสนี้เรียกว่า “ขุดหลุมฝังตัวเอง” เกิดการแสดงขนาดภาพเพี้ยนในเกมส์ จากการพยายาม swap ภาพเองเมื่อ resolution ของ devices สูง

จากกำหนดการแสดงผลของ Application ไว้ที่ resolution ต่ำๆ เช่น 320×480 และทำการแสดงผลภาพบน retina display / ipad corona จะทำการปรับภาพเป็น dynamic scale โดยหากภาพที่ใช้การโหลดรูปแบบ newImageRect() จะได้รับการเปลี่ยนภาพให้โดยอัตโนมัติ เป็นชุด 2x แต่ภาพที่โหลดแบบ newImage() ต้องใส่ parameter เพื่อบังคับให้แสดงแบบไม่ dynamic scale เพิ่ม เมื่อทำการเปลี่ยนภาพเองผ่านโปรแกรม !!

ที่สำคัญ คือ simulator จะไม่แสดง error แต่เมื่อทดสอบใน devices จึงจะพบ…T T

Scale 9 Grid with Gradient work around!!

Today I’ve try to create a scale 9 shape with gradient fill in its shape and found that when create the scale 9 from IDE the gradient will not slice into scale 9 properly. So the result look like it is stretch. The work around that I found after googling for a while is use “Union” command to the out side border of the scale 9 by select menu “Modify/Objects/Union” and it work !! :)

การอ่านค่า flashvars จาก swf ที่ถูกโหลดเข้ามา

ปัญหาในการพยายามโหลดค่า flashvars จาก swf ที่ถูกโหลดเข้ามาภายใน swf อีกตัว เป็นปัญหาที่เรียกได้ว่าน่าปวดหัวไม่น้อยทีเดียว แต่จริงๆ แล้ววิธีแก้ปัญหานั้นไม่ยากครับ หลังจากที่ผมพยายามแก้ปัญหาและหาข้อมูลอยู่พักใหญ่ ในที่สุดก็ได้ข้อสรุป คือ

ที่ตัว Main นะครับ เวลาอ่านค่า flashvars จะใช้คำสั่ง

ในขณะที่ถ้าต้องการอ่านค่า flashvars จาก swf ที่ถูกโหลดเข้ามาจะต้องเปลี่ยนคำสั่งเป็น

การเรียกผ่านตัวแปร stage จะเป็นการเรียกไปยัง display container ของ swf ที่ถูกโหลดเข้ามานั่นเอง

และหากยังไม่ได้ ก็ยังมีอีกวิธี โดยการส่งค่าตัวแปรต่างๆ เข้าไปตอนโหลด swf อีกตัวนั่นเอง

var myRequest:URLRequest = new URLRequest(String(“/myPath.swf” + fVars));

ที่มา: http://ragona.com/blog/pass-flashvars-loaded-swf/

Today I want to add Hibernate feature to Vista?

And I can’t find it in the control panel / power management section. After google around then I found the solution.

To disable hibernation:
Click Start. Type “powercfg /hibernate off” and then Press Ctrl+Shift+Enter.
To enable hibernation:
Click Start. Type “powercfg /hibernate on” and then Press Ctrl+Shift+Enter.

A command prompt will appear and disappear. The program doesn’t give you any feedback anyway, so you’re not missing anything.
By press Ctrl+Shift+Enter is a shortcut to run a program as an Administrator.

ฟันธงโหลดภาษาไทยใน XML ด้วย AS3 จาก ServerSide

หัวข้อนี้ไม่เขียนภาษาไทยคงจะแปลก เพราะเกี่ยวกับการดึงข้อมูลภาษาไทย จากไฟล์ XML หรือจากการส่งข้อมูลจาก Server แบบ XML Format เรื่องของเรื่องคือ พอดีได้มีโอกาสต้องทำงานดึงข้อมูลภาษาไทยมาแสดงบนหน้าเว็บ ซึ่งเนื้องานที่ทำอยู่ทุกที ไม่ค่อยได้ใช้ภาษาไทยเท่าไหร่ สรุปคือ เกิดปัญหาขึ้น แสดงได้บ้าง ไม่ได้บ้าง มี random character แปลกๆ ต่อท้ายข้อมูลที่โหลดมาบ้้าง ทำให้เกิด XML not well-formed ฟ้องขึ้นมา (ตัวอย่าง well formed xml) สุดท้ายไ้ด้ความช่วยเหลือจาก katopz และ joke และทำให้แก้ปัญหาไปได้ จึงอยากเอามาแบ่งปัน เผื่อว่าจะช่วยลดเวลาในการแก้ปัญหาของท่านอื่นๆ ได้ต่อไป ดังนี้

1. ฝั่ง flash โหลด XML ตามปกติ แต่ห้ามใช้ System.useCodePage = true; ถ้ามีให้ comment ทิ้งไป เนื่องจากเราจะใช้การ encode ข้อมูลแบบ UTF-8 ซึ่งจะกำหนดมากจาก xml ที่โหลดเข้ามา หากกำหนดให้ใช้ System.useCodePage จะเป็นการบังคับให้แฟลชใช้การ encode ข้อมูลตาม System default ของเครื่องผู้ใช้ ซึ่งเมื่อส่งข้อมูลมาแบบ UTF-8 แน่นอนว่าภาษาไทยมันจะแสดงไม่ได้อย่างแน่นอน และการเตรียมการฝั่ง Flash จะจบเพียงแค่นี้ (จริงๆ)

ส่วนวิธีการอ่านข้อมูล XML ลองอ่านกระทู้ของน้องก่อ ดูนะครับ เขียนไว้ละเอียดดีครับ
- ทำงานกับฐานข้อมูลแบบ Online ด้วย Flash และ PHP
- Flash+XML ตอนที่ 1

2. ถัดมาส่วนที่เป็นเทคนิคสำคัญในการที่จะทำให้ส่งข้อมูลภาษาไทยสำเร็จ คือ ฝั่ง XML นี่เอง ที่สำคัญอย่างยิ่ง ดังนี้

2.1 ข้อมูลที่ส่งมาทั้งหมด ต้อง encode เป็นแบบ UTF-8 (วิธีแปลงไฟล์เพื่อทดสอบง่ายๆ คือ ใช้ notepad เปิด xml ไฟล์ก่อน หลังจากนั้นเลือก Save As และเลือก Encoding เป็น UTF-8 ก็จะได้ไฟล์ไว้ทดสอบ – credit: katopz)

2.2 ข้อมูลภาษาไทยทั้งหมด ควรที่จะเก็บอยู่ภายใน tag value ของ xml ไม่ใช่ attribute และครอบข้อมูลภาษาไทยทั้งหมดด้วย <![CDATA[ภาษาไทย]]]> (credit: katopz และ joke) เช่น

<xml>
<title><![CDATA[กรุงเทพมหานคร]]></title>
<link>http://www.bangkok.com</link>
</xml>

2.3 ใส่ header ของ XML Data ที่ส่งกลับมาเป็น <?xml version=”1.0″ encoding=”utf-8″ ?>

2.4 หากยังไม่ได้ ให้ตรวจสอบอีกครั้งว่าข้อมูลที่ส่งมา encode เป็น UTF-8

2.5 หากยังไม่ได้ ให้ตรวจสอบอีกครั้งว่าข้อมูลที่ส่งมา encode เป็น UTF-8 และไม่ได้ encode ซ้ำซ้อนเพราะจะทำให้ข้อมูลผิดพลาดได้

2.6 หากยังไม่ได้ ทดสอบด้วยไฟล์ที่ได้มากจาก notepad และ encode ด้วย UTF-8

ถ้าทำตามข้างบนทั้งหมด ผมมั่นใจว่าน่าจะได้อย่างแน่นอนครับ และขอบคุณ katopz และ joke อีกครั้งสำหรับเทคนิคดีๆ ครับ

Useful HexColor Extract Code

This code help me solve many cool effect and also shorten my writing. Also sometime it is easily to forget so here is another good tips to note on here :)

var colour:uint = 0xFF297400;
var A:Number = colour >> 24 & 0xFF;
var R:Number = colour >> 16 & 0xFF;
var G:Number = colour >> 8 & 0xFF;
var B:Number = colour & 0xFF;

// RGB
var colourRBG:uint = (R << 16 | G << 8 | B);

// ARGB
var colourARBG:uint = (A << 24 | R << 16 | G << 8 | B);

Radians & Degrees

This math always come back to me after a certain of time and take me 5 – 10 minutes to think or look around the correct one. So I write this down for myself and others for save time to look around.

radians = degrees * Math.PI / 180
degrees = radians * 180 / Math.PI

P.S.
360 degrees = 2 * Math.PI (radian)